<?php

namespace app\common\command;

use app\lib\redis\Redis;
use think\Exception;

class Meeting
{

    /**
     * 会议提醒 加入redis队列
     */
    public static function writeQueue(){
        //trace('会议提醒同步开始','info');

        try {
            $model = new \app\admin\model\Joinmeeting();

            $where = [
                //'a.isConfirm' => 1,
                'b.startTime' => ['gt', time()]
            ];
            $join = [
                ['__MEETING__ b', 'b.id = a.meetingId'],
                ['__MEMBER__ m', 'm.id = a.memberId']
            ];
            $field = 'a.id as joinId, a.memberId, b.id as meetingId, b.title, b.startTime, b.address, b.remind, m.mobile';
            // 所有还未开始的会议
            $list = $model->alias('a')->where($where)->join($join)->field($field)->select();
            if($list){
                $redis = Redis::getRedisConn();
                foreach ($list as $item) {
                    trace($item['title'] . '|' . date('Y-m-d H:i:s', $item['startTime']),'info');

                    // 会议开始时间
                    $startTime = $item['startTime'];
                    // 提醒时间
                    $endTime = $startTime - ($item['remind'] * 60);

                    // 是否需要提醒
                    if(date('Y-m-d H:i', time()) === date('Y-m-d H:i', $endTime)){
                        // 短信提醒
                        $data = [];
                        $data['mobile'] = $item['mobile'];
                        $data['joinId'] = $item['joinId'];
                        $data['title'] = $item['title'];
                        $data['startTime'] = $startTime;
                        $data['address'] = $item['address'];
                        if ($data['mobile']) {
                            $redis->RPush('meeting_mobile_list', json_encode($data));
                        } else {
                            trace('没有手机号','notice');
                        }
                    }
                }
            }
        } catch (Exception $e) {
            trace($e->getMessage(),'error');
        }

        //trace('会议提醒同步结束','info');
    }

    /**
     * 执行 会议提醒
     */
    public static function cron(){
        self::sendSms();
    }

    /**
     * 发送会议提醒短信
     */
    private static function sendSms(){
        $Redis = Redis::getRedisConn();
        $smsObj = new \app\lib\Sms();

        $meetingData = $Redis->LRange('meeting_mobile_list',0,-1);
        if($meetingData){
            try {
                foreach ($meetingData as $data){
                    $data = json_decode($data,true);
                    $message = '';
                    $message .= "会议提醒：\n会议主题：" . $data['title'] . "\n";
                    $message .= '会议开始时间：' . date('Y-m-d H:i', $data['startTime']) . "\n";
                    $message .= '会议地点：' . $data['address']. "\n";
                    $message .= '请务必准时参加 【智慧乡镇】';

                    $smsRes = $smsObj->sendMess($data['mobile'], $message);
                    if (isset($smsRes)) {
                        if ($smsRes['msg'] == 'ok') {
                            // TODO 提醒数 +1
                            //\app\admin\model\Joinmeeting::where('id', $data['joinId'])->setInc('countRemind');

                            $Redis->LRem('meeting_mobile_list', json_encode($data),-1);
                            trace($data['mobile'] . ': 会议提醒短信发送成功','log');
                        } else {
                            trace($data['mobile'] . ': 会议提醒短信发送失败','notice');
                        }
                    }
                }
            } catch (Exception $e) {
                trace($e->getMessage(),'error');
            }
        }
    }

}